package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAllRole() {
        return roleDao.findAllRole();
    }

    @Override
    public List<Menu> findRoleByUsername(String username) {
        //创建新的集合返回给controller
        List<Menu> menuList = new ArrayList<>();
        //查询到所有的menu对象集合
        List<Menu> list = roleDao.findRoleByUsername(username);//这里没有children
        List<Menu> children = new ArrayList<>();
        //遍历集合
        for (Menu menu : list) {
            //获得所有对应的父菜单id
            if (menu.getParentMenuId()==null){
                //父菜单id与子菜单id相等
                Integer id = menu.getId();
                children = roleDao.findChildren(id);
                //查询子菜单
                menu.setChildren(children);
                menuList.add(menu);
            }
        }
        return menuList;
    }

    @Override
    public void add(Role role, Integer[] permissionIds) {
        roleDao.add(role);
        setRelation(role.getId(), permissionIds);
    }


    public PageResult findPage(QueryPageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        Page<Role> page = roleDao.findByCondition(pageBean.getQueryString());
        return new PageResult(page.getTotal(), page);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }


    @Override
    @Transactional
    public void edit(Role role, Integer[] permissionIds) {
        roleDao.delRelation(role.getId());
        //维护新的关系
        setRelation(role.getId(), permissionIds);
        //编辑检查组
        roleDao.edit(role);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public void delById(Integer id) {
        long count = roleDao.findCountById(id);
        if (count > 0) {
            throw new BusinessRuntimeException("该角色被菜单关联，不能删除！！");
        }
        roleDao.delRelation(id);
        roleDao.delById(id);
    }

    //设置检查组合和检查项的关联关系
    private void setRelation(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                roleDao.setRelation(roleId, permissionId);
            }
        }

    }
}
